package com.csnf.servlet;

import com.csnf.dao.UserDao;
import com.csnf.entity.User;
import com.csnf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet({"/getUser", "/editUser","/updateUser","/deleteUser"})
public class UserServlet extends HttpServlet {
    UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path=req.getServletPath();
        switch (path){
            case "/getUser":
                getUserByNickName(req,resp);
                break;
            case "/editUser":
                getUserById(req,resp);
                break;
            case "/deleteUser":
                doPut(req,resp);
                break;

        }
    }
    private void getUserByNickName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String query = req.getParameter("queryName");
        query = query==null?"":query;
        List<User> userList = null;
        try {
             userList = userService.getUserNickName(query);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("userList",userList);
        req.getRequestDispatcher("success.jsp").forward(req,resp);
    }
    private void getUserById(HttpServletRequest req, HttpServletResponse resp){
        String user_id = req.getParameter("user_id");
        try {
            User user = userService.getUserById(Integer.parseInt(user_id));
            req.setAttribute("user",user);
            req.getRequestDispatcher("editUser.jsp").forward(req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resq) throws ServletException, IOException {
        Integer user_id=Integer.valueOf(req.getParameter("user_id"));
        String user_name=req.getParameter("user_name");
        String user_nickname=req.getParameter("user_nickName");
        boolean flag=false;
        try {
            flag=UserService.updateUser(new User(user_id,user_name,user_nickname));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(flag){
            getUserByNickName(req,resq);
        }else {
            req.setAttribute("msg","fail");
            req.getRequestDispatcher("editUser.jsp").forward(req,resq);
        }
    }
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer user_id=Integer.valueOf(req.getParameter("user_id"));
        Boolean flag=false;
        try {
            flag=UserService.deleteUser(user_id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(flag){
            req.setAttribute("msg","删除成功");


        }else {
            req.setAttribute("msg","删除失败");

        }
        getUserByNickName(req,resp);
    }
}
